package com.micro.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.micro.entity.TrainMealOrder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;

import java.util.List;

@Mapper
public interface TrainMealOrderRepository extends BaseMapper<TrainMealOrder> {
    
    // 根据用户ID查询订单基本信息
    @Select("SELECT * FROM 火车餐订单 WHERE 用户ID = #{userId} ORDER BY 创建时间 DESC")
    List<TrainMealOrder> findByUserId(@Param("userId") Long userId);
    
    // 使用原生SQL查询，明确指定字段名
    @Select("SELECT 订单ID, 用户ID, 列车ID, 座位号, 订单编号, 订单总金额, 订单状态, 创建时间, 支付时间, 备注 FROM 火车餐订单 WHERE 用户ID = #{userId} ORDER BY 创建时间 DESC")
    @Results({
        @Result(property = "orderId", column = "订单ID"),
        @Result(property = "userId", column = "用户ID"),
        @Result(property = "trainId", column = "列车ID"),
        @Result(property = "seatNumber", column = "座位号"),
        @Result(property = "orderNumber", column = "订单编号"),
        @Result(property = "totalAmount", column = "订单总金额"),
        @Result(property = "status", column = "订单状态"),
        @Result(property = "createTime", column = "创建时间"),
        @Result(property = "paymentTime", column = "支付时间"),
        @Result(property = "remark", column = "备注")
    })
    List<TrainMealOrder> findByUserIdWithExplicitFields(@Param("userId") Long userId);
    
    // 根据订单ID查询订单详情
    @Select("SELECT * FROM 火车餐订单 WHERE 订单ID = #{orderId}")
    TrainMealOrder findOrderWithDetails(@Param("orderId") Long orderId);
    
    // 根据订单状态查询订单
    @Select("SELECT * FROM 火车餐订单 WHERE 订单状态 = #{status}")
    List<TrainMealOrder> findByStatus(@Param("status") String status);
    
    // 根据列车ID查询订单
    @Select("SELECT * FROM 火车餐订单 WHERE 列车ID = #{trainId}")
    List<TrainMealOrder> findByTrainId(@Param("trainId") Long trainId);
    
    // 根据订单编号查询订单
    @Select("SELECT * FROM 火车餐订单 WHERE 订单编号 = #{orderNumber}")
    TrainMealOrder findByOrderNumber(@Param("orderNumber") String orderNumber);
    
    // 根据订单ID查询订单详情（使用明确的字段映射）
    @Select("SELECT 订单ID, 用户ID, 列车ID, 座位号, 订单编号, 订单总金额, 订单状态, 创建时间, 支付时间, 备注 FROM 火车餐订单 WHERE 订单ID = #{orderId}")
    @Results({
        @Result(property = "orderId", column = "订单ID"),
        @Result(property = "userId", column = "用户ID"),
        @Result(property = "trainId", column = "列车ID"),
        @Result(property = "seatNumber", column = "座位号"),
        @Result(property = "orderNumber", column = "订单编号"),
        @Result(property = "totalAmount", column = "订单总金额"),
        @Result(property = "status", column = "订单状态"),
        @Result(property = "createTime", column = "创建时间"),
        @Result(property = "paymentTime", column = "支付时间"),
        @Result(property = "remark", column = "备注")
    })
    TrainMealOrder findOrderDetailsById(@Param("orderId") Long orderId);
}
